package ec.atreasure.com.core.net;

import android.content.Context;

import java.io.File;
import java.util.WeakHashMap;

import ec.atreasure.com.core.net.callback.IError;
import ec.atreasure.com.core.net.callback.IFailure;
import ec.atreasure.com.core.net.callback.IRequest;
import ec.atreasure.com.core.net.callback.ISuccess;
import ec.atreasure.com.core.ui.LoaderStyle;
import okhttp3.RequestBody;

/**
 * Description : RestClient建造者
 * Created by wangjin on 2019/6/23.
 * Job number：
 * Phone ：18301070822
 * Email： 120182051@qq.com
 * Person in charge : 汪渝栋
 * Leader：
 */
public final class RestClientBuilder {

    //请求参数
    private final WeakHashMap<String, Object> params = new WeakHashMap<>();

    //请求地址
    private String url = null;

    //请求开始/结束回调
    private IRequest requestCallback = null;

    //请求错误回调
    private IError errorCallback = null;

    //请求失败回调
    private IFailure failureCallback = null;

    //请求成功回调
    private ISuccess successCallback = null;

    //post raw/put raw请求体
    private RequestBody body = null;

    //加载过度动画
    private LoaderStyle loaderStyle = null;

    //具体显示上下文具体到fragment或者activity
    private Context context = null;

    //上传文件
    private File uploadFile = null;

    //下载文件目录
    private String downloadDir = null;

    //下载文件扩展名
    private String extension = null;

    //下载文件名
    private String downloadFileName = null;

    RestClientBuilder() {
    }

    public final RestClientBuilder withUrl(String url) {
        this.url = url;
        return this;
    }

    public final RestClientBuilder withParams(WeakHashMap params) {
        this.params.putAll(params);
        return this;
    }

    public final RestClientBuilder setRequestCallback(IRequest iRequest) {
        this.requestCallback = iRequest;
        return this;
    }

    public final RestClientBuilder setErrorCallback(IError iError) {
        this.errorCallback = iError;
        return this;
    }

    public final RestClientBuilder setFailureCallback(IFailure iFailure) {
        this.failureCallback = iFailure;
        return this;
    }

    public final RestClientBuilder setSuccessCallback(ISuccess iSuccess) {
        this.successCallback = iSuccess;
        return this;
    }

    public final RestClientBuilder setLoader(Context context, LoaderStyle loaderStyle) {
        this.context = context;
        this.loaderStyle = loaderStyle;
        return this;
    }

    public final RestClientBuilder setLoader(Context context) {
        this.context = context;
        this.loaderStyle = LoaderStyle.BallSpinFadeLoaderIndicator;
        return this;
    }

    public final RestClientBuilder setUploadFile(File uploadFile) {
        this.uploadFile = uploadFile;
        return this;
    }

    public final RestClientBuilder setUploadFile(String uploadFilePath) {
        this.uploadFile = new File(uploadFilePath);
        return this;
    }

    public final RestClientBuilder setRequestBody(RequestBody requestBody) {
        this.body = requestBody;
        return this;
    }

    public final RestClientBuilder setDownloadFileName(String downloadFileName) {
        this.downloadFileName = downloadFileName;
        return this;
    }

    public final RestClientBuilder setDownloadFileDir(String downloadFileDir) {
        this.downloadDir = downloadFileDir;
        return this;
    }

    public final RestClientBuilder setDownloadFileExtension(String extension) {
        this.extension = extension;
        return this;
    }


    public final RestClient build() {
        return new RestClient(params, url, requestCallback, errorCallback, failureCallback,
                successCallback, body, loaderStyle, uploadFile,downloadDir,extension,downloadFileName,context);
    }


}
